package developer.zyk.ability.knowledge.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import developer.zyk.ability.knowledge.KnowledgeClosedLoop;
import developer.zyk.ability.knowledge.bo.OperateRecord;
import developer.zyk.ability.knowledge.common.KnowledgeTypeEnum;
import developer.zyk.ability.knowledge.common.KnowledgeUploadStatus;
import developer.zyk.ability.knowledge.dao.entity.ChatKnowledgeRecordDO;
import developer.zyk.ability.knowledge.dao.mappers.ChatKnowledgeRecordMapper;
import developer.zyk.ability.knowledge.param.ChatKnowledgeSearchParam;
import developer.zyk.ability.knowledge.param.ChatKnowledgeSubmitParam;
import developer.zyk.ability.knowledge.param.ChatKnowledgeTextParam;
import developer.zyk.global.user.bo.UserInfo;
import developer.zyk.global.user.helper.UserInfoHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @author zhangyongkang
 * @time 2025/5/14 16:56
 * @description
 */
@Component
public class ChatKnowledgeService {
    @Resource
    private ChatKnowledgeRecordMapper chatKnowledgeRecordMapper;

    //添加记录
    public List<Long> addRecord(ChatKnowledgeSubmitParam chatKnowledgeSubmitParam, KnowledgeTypeEnum knowledgeTypeEnum) {
        UserInfo userInfo = UserInfoHelper.getUserInfoById(chatKnowledgeSubmitParam.getUserId());
        List<ChatKnowledgeRecordDO> list = Optional.ofNullable(chatKnowledgeSubmitParam.getFileList()).orElse(new ArrayList<>())
                .stream()
                .map(a -> {
                    ChatKnowledgeRecordDO chatKnowledgeRecordDO = new ChatKnowledgeRecordDO();
                    chatKnowledgeRecordDO.setFileId(a);
                    chatKnowledgeRecordDO.setKnowledgeType(knowledgeTypeEnum.getType());

                    chatKnowledgeRecordDO.setBelongUserId(chatKnowledgeSubmitParam.getUserId());
                    chatKnowledgeRecordDO.setCreateBy(chatKnowledgeSubmitParam.getUserId());
                    chatKnowledgeRecordDO.setStatus(KnowledgeUploadStatus.INIT.getCode());
                    chatKnowledgeRecordDO.setStatusText(KnowledgeUploadStatus.INIT.getName());
                    chatKnowledgeRecordDO.setModifyTime(LocalDateTime.now());
                    return chatKnowledgeRecordDO;
                })
                .toList();

        chatKnowledgeRecordMapper.insert(list);
        return list.stream()
                .filter(Objects::nonNull)
                .map(ChatKnowledgeRecordDO::getId)
                .peek(a -> {
                    //记录闭环
                    KnowledgeClosedLoop.record(
                            a,
                            new OperateRecord(
                                    UUID.randomUUID().toString(),
                                    userInfo,
                                    KnowledgeUploadStatus.INIT.getCode(),
                                    KnowledgeUploadStatus.INIT.getName(),
                                    ""
                            ),
                            userInfo,
                            KnowledgeUploadStatus.INIT
                    );
                })
                .toList();
    }

    public Long addOrUpdateTextRecord(
            ChatKnowledgeTextParam chatKnowledgeTextParam,
            KnowledgeTypeEnum knowledgeTypeEnum
    ) {
        UserInfo userInfo = UserInfoHelper.getUserInfoById(chatKnowledgeTextParam.getUserId());
        ChatKnowledgeRecordDO chatKnowledgeRecordDO = new ChatKnowledgeRecordDO();
        chatKnowledgeRecordDO.setKnowledgeType(knowledgeTypeEnum.getType());
        chatKnowledgeRecordDO.setBelongUserId(userInfo.getUserId());
        chatKnowledgeRecordDO.setCreateBy(userInfo.getUserId());
        chatKnowledgeRecordDO.setContent(chatKnowledgeTextParam.getContent());
        chatKnowledgeRecordDO.setTitle(chatKnowledgeTextParam.getTitle());
        chatKnowledgeRecordDO.setStatus(KnowledgeUploadStatus.INIT.getCode());
        chatKnowledgeRecordDO.setStatusText(KnowledgeUploadStatus.INIT.getName());
        chatKnowledgeRecordDO.setModifyTime(LocalDateTime.now());
        if (chatKnowledgeTextParam.getRecordId() != null) {
            chatKnowledgeRecordDO.setId(chatKnowledgeTextParam.getRecordId());
            chatKnowledgeRecordMapper.updateById(chatKnowledgeRecordDO);
        } else {
            chatKnowledgeRecordMapper.insert(chatKnowledgeRecordDO);
        }
        KnowledgeClosedLoop.record(
                chatKnowledgeRecordDO.getId(),
                new OperateRecord(
                        UUID.randomUUID().toString(),
                        userInfo,
                        KnowledgeUploadStatus.INIT.getCode(),
                        KnowledgeUploadStatus.INIT.getName(),
                        ""
                ),
                userInfo,
                KnowledgeUploadStatus.INIT
        );
        return chatKnowledgeRecordDO.getId();
    }

    public List<ChatKnowledgeRecordDO> search(ChatKnowledgeSearchParam chatKnowledgeSearchParam) {
        LambdaQueryWrapper<ChatKnowledgeRecordDO> queryWrapper = new QueryWrapper<ChatKnowledgeRecordDO>().
                lambda()
                .eq(Objects.nonNull(chatKnowledgeSearchParam.getUserId()), ChatKnowledgeRecordDO::getBelongUserId, chatKnowledgeSearchParam.getUserId())
                .eq(Objects.nonNull(chatKnowledgeSearchParam.getKnowledgeType()), ChatKnowledgeRecordDO::getKnowledgeType, chatKnowledgeSearchParam.getKnowledgeType());
        return chatKnowledgeRecordMapper.selectList(queryWrapper);
    }


    public ChatKnowledgeRecordDO getById(Long id) {
        return chatKnowledgeRecordMapper.selectById(id);
    }


    public boolean deleteById(Long id) {
        return chatKnowledgeRecordMapper.deleteById(id) > 0;
    }


    /**
     * 修改状态
     *
     * @param id
     * @param enable
     * @return
     */
    public boolean updateEnableStatus(long id, Integer enable) {
        ChatKnowledgeRecordDO ChatKnowledgeRecordDO = new ChatKnowledgeRecordDO();
        ChatKnowledgeRecordDO.setId(id);
        ChatKnowledgeRecordDO.setEnable(enable);

        int i = chatKnowledgeRecordMapper.updateById(ChatKnowledgeRecordDO);
        return i > 0;
    }

}
